home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / lh211src.zip / FILES.C < prev    next >
C/C++ Source or Header  |  1991-02-26  |  5KB  |  210 lines

  1. /***********************************************************
  2.     files.c -- collect filenames
  3. ***********************************************************/
  4. #include <stddef.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <farstr.h>
  8. /*
  9. #include <io.h>
  10. */
  11. #include <dos.h>
  12. #include <ctype.h>
  13. #include "lh.h"
  14. #include "errmes.h"
  15.  
  16. #define MAXPATH 65
  17.  
  18. int tcount, fcount;
  19.  
  20. struct fb far *fbuf;
  21. static struct fb far *flst;
  22.  
  23. /***************************************
  24.     initialize pointers for 
  25.     registing files
  26. ***************************************/
  27. static void init_regfile(void)
  28. {
  29.     fbuf = flst = NULL;
  30.  
  31.     tcount = fcount = 0;
  32. }
  33.  
  34. /***************************************
  35.     regist file name
  36. ***************************************/
  37. static void regfile(char *p, char *q, char *f, long utc)
  38. /*
  39.     p: directory name including drive & base directory
  40.     q: partial directory name following base directory
  41.     f: file name
  42.  
  43.     "d:pppp/qqqq/\0 ffff\0"
  44.      ^      ^       ^
  45.      p      q       f
  46. */
  47. {
  48.     struct fb far *fp, far *fnxt;
  49.     char far *r, far *t;
  50.  
  51.     if (strcmp(f, lhtmp1) == 0 || strcmp(f, lhtmp2) == 0)
  52.         return;                    /* temporary file ? */
  53.     if (flg_i == 0) {
  54. /*
  55.         strupr(p);
  56. */
  57.         char *s;
  58.  
  59.         s = p;
  60.         while (*s) {
  61.             if (iskanji(*s)) s++;
  62.             else *s = toupper(*s);
  63.             if (*s) s++;
  64.         }
  65.     }
  66.     fnxt = e_farmalloc(strlen(p) + strlen(f) + sizeof(struct fb));
  67.     fnxt -> time = utc;
  68.     far_strcpy(r = fnxt -> fname, p);
  69.     far_strcpy(t = fnxt -> fpos = r + strlen(p), f);
  70.     if (flg_x) t = r + (q - p);
  71.     fnxt -> cpos = t;
  72.  
  73.     for (fp = fbuf; fp != NULL; fp = fp -> nxt) {
  74.         if (far_strcmp(fp -> cpos, t) == 0)    {
  75.             if (far_strcmp(fp -> fname, r) == 0) {
  76.                 farfree(fnxt);
  77.                 return;
  78.             } else {
  79.                 far_strcpy(work, t);
  80.                 error(DUPFNERR, work);
  81.                             /* same registing names of different files */
  82.             }
  83.         }
  84.     }
  85.     if (flst) {
  86.         flst -> nxt = fnxt;
  87.     } else {
  88.         fbuf = fnxt;
  89.     }
  90.     flst = fnxt;
  91.     flst -> nxt = NULL;
  92.     flst -> used = 0;
  93.  
  94.     tcount ++;
  95. }
  96.  
  97. /***************************************
  98.   recursive collection of files
  99. ***************************************/
  100. static int travel(char *p, char *q, char *f)
  101. /*
  102.     p: directory name including drive & base directory
  103.     q: directory name following base directory
  104.     f: file name
  105.  
  106.     "pppp/qqqq/\0 ffff\0"
  107.      ^    ^       ^
  108.      p    q       f
  109. */
  110. {
  111.     struct find_t srchbuf;
  112.     char *r, *s;
  113.     int done, cnt;
  114.  
  115. /*
  116.     if (f) strupr(f);
  117. */
  118.     cnt = 0;
  119.     convdelim(p, pathdelim);
  120.     done = _dos_findfirst(p, 0x17, &srchbuf);    /* search the first file */
  121.     convdelim(p, DELIM);
  122.     s = backpath(q);
  123.     while (! done) {
  124.         if (!(srchbuf.attrib & 0x06) || flg_a) {
  125.             if (srchbuf.attrib & 0x10) {    /* if this is a sub-directory */
  126.                 if (flg_r) {
  127.                     if (srchbuf.name[0] != '.') {
  128.                         r = stpcpy(stpcpy(s, srchbuf.name), DELIMSTR "*.*");
  129.                         if (r - p > MAXPATH)
  130.                             error(TOOLONGERR, p);
  131.                         cnt += travel(p, q, f);
  132.                             /* search recursively */
  133.                         *s = '\0';
  134.                     }
  135.                 }
  136.             } else {                        /* if this is a file */
  137.                 if (flg_r != 1 
  138.                     || *matchfname(f, srchbuf.name) == '\0')
  139.                 {
  140.                     cnt++;
  141.                     regfile(p, q, srchbuf.name, 
  142.                             dos2unix((struct ftime *)&(srchbuf.wr_time)));
  143.                 }
  144.             }
  145.         }
  146.         done = _dos_findnext(&srchbuf);
  147.     }
  148.     return cnt;                            /* number of registed files */
  149. }
  150.  
  151. /*******************************
  152.     make file lists to append
  153. *******************************/
  154. void mklist(void)
  155. {
  156.     char far *p;
  157.     char *q, *r;
  158.     int cnt, Nfile;
  159.     struct pat far *pt;
  160.     char fname[13];
  161.  
  162.     Nfile = 0;
  163.     init_regfile();
  164.     for (pt = pbuf -> nxt; pt != NULL; pt = pt -> nxt) {
  165.         p = pt -> pname;
  166.         r = work;
  167.         if (p[1] == ':') {            /* if file name includes drive */
  168.             far_strcpy(r, p);            /* ignore base directory */
  169.             r += 2;                        /* don't regist drive name */
  170.         } else {
  171.             if ((uchar)*p != DELIM) {
  172.                 r = (char *)far_stpcpy(r, pt -> bdir);
  173.             } else if (*(pt -> bdir) && (pt -> bdir)[1] == ':') {
  174.                 far_strcpy(r, pt -> bdir);
  175.                 r += 2;
  176.             }
  177.             far_strcpy(r, p);
  178.         }
  179.         if ((q = strchr(work, '+')) != NULL) {
  180.             strcpy(q, "*.*");
  181.         }
  182.         q = getfilename(work);
  183.         if (flg_r == 1) {            /* /r+ mode */
  184.             strcpy(fname, q);
  185.             strcpy(q, "*.*");
  186.             cnt = travel(work, r, fname);
  187.         } else {
  188.             if (flg_r == 2) {                /* /r2 mode */
  189.                 if (*q == '.')
  190.                     strcat(r, DELIMSTR "*.*");
  191.             }
  192.             cnt = travel(work, r, NULL);
  193.         }
  194.         Nfile += pt -> cnt = cnt;
  195.     }
  196. }
  197.  
  198. struct fb far *searchfile(char *path)
  199. {
  200.     struct fb far *fp;
  201.  
  202.     for (fp = fbuf; fp != NULL; fp = fp -> nxt) {
  203.         if (far_strcmp(path, fp -> cpos) == 0){
  204.             fp -> used = 1;
  205.             return fp;
  206.         }
  207.     }
  208.     return NULL;
  209. }
  210.